<?php
use think\facade\Log;
use app\admin\model\SystemParamValue;
// 应用公共文件

//错误级别
error_reporting(E_ERROR | E_PARSE);

/**
 * 获取系统参数
 * @param  array  $data 被认证的数据
 * @return string       签名
 */
function getSystem($paramCode,$parentValue=-1)
{
	$param["param_code"] = $paramCode;
	if($parentValue!=-1){
		$param["parent_value"] = $parentValue;
	}
	$value = SystemParamValue::where($param)->value("param_value");
	if( $value === null  && $parentValue !=-1 ){
		$param["parent_value"] = 0;
		$value = SystemParamValue::where($param)->value("param_value");
	}
	return  $value;
}



/**
 * 获取当前路径
 * @param  null
 */
function getActiveUrl(){
	return app('http')->getName().'/'.request()->pathinfo();
}



/**
 * 数据库多语句查询
 * @param  $sqls
 */
function db_multi_query($sqls=""){
	$data=array();
	$c = config()['database'];
	$c = $c['connections']['mysql'] ?? $c['connections']['mysqi'] ?? [];
	$conn = new \mysqli( $c['hostname'], $c['username'],$c['password'],$c['database'],$c['hostport']);
	if (mysqli_connect_errno($conn)){
		throw new \think\Exception( "连接到 MySQL 失败: " . mysqli_connect_error() );
	}else{
		$conn->query("set names utf8");
		if(mysqli_multi_query($conn, $sqls)) {
	        do {
	            if($result = mysqli_store_result($conn)) {
	                while( $row = mysqli_fetch_assoc($result)  ) {
	                	array_push($data, $row);
	                }
	                mysqli_free_result($result);        
	            }
	        } while(mysqli_next_result($conn));
	    }
		if($conn->error){
			mysqli_close($conn);
			throw new \think\Exception( 'error:  ' . $conn->error );
		}
	    mysqli_close($conn);
	}
	return $data;
}


/**
 * 去掉？后的参数
 * @param  string $url 
 */
function clear_urlcan($url){
    $rstr='';
    $tmparr = parse_url($url);
    $rstr = empty($tmparr['scheme']) ? '' : $tmparr['scheme'].'://';
    $rstr.= $tmparr['host'] ?? '' .$tmparr['path'] ?? '';
    return $rstr;
}


/**
 * 添加日志
 * @param  null
 */
function info($data){
	Log::info( $data );
}

